$import( "SmitearWebGL.Objects.Parser.Collada.Const" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeDocument" );
$import( "SmitearWebGL.Events.IOErrorEvent" );
$import( "SmitearWebGL.Events.ProgressEvent" );
$import( "SmitearWebGL.Events.TimerEvent" );
$import( "SmitearWebGL.Events.EventDispatcher" );
$import( "SmitearWebGL.Utils.Timer" );

SmitearWebGL.Objects.Parser.Collada.DaeReader = SmitearWebGL.Events.EventDispatcher.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaeReader" );

		this._sourceTimer = new SmitearWebGL.Utils.Timer(1);
		this._sourceTimer.addEventListener( SmitearWebGL.Events.TimerEvent.TIMER, this, this.loadNextSource );

		this.url = "";
		this.baseUrl = "";
		this.document = null;
		this.parseMessage = "";
	}
	,
	load : function( url )
	{
		this.baseUrl = url;

		if ( url.indexOf("/") != -1 )
		{
			var parts = url.split("/");
			parts.pop();
			this.baseUrl = parts.join("/");
		}

		$LOG( "reading: " + url );

		this.parseMessage = "reading COLLADA";

		if( this._sourceTimer.isRunning() )
		this._sourceTimer.stop();

		this.url = url;
		var docurl = url + "?_=" + new Date().getTime();
		var self = this;

		if( SmitearWebGL.Objects.Parser.Collada.ColladaDocuments[url] )
		{
			this.document = SmitearWebGL.Objects.Parser.Collada.ColladaDocuments[url];
		}
		else
		{
			var req = new XMLHttpRequest();
			if( req )
			{
				req.overrideMimeType( "text/xml" );
				req.onreadystatechange = function()
				{
					if( this.readyState  == 4 )
					{
						if( this.status  == 200 || this.status == 0 )
						{
							self._loadDocument( this.responseXML.documentElement );
						}
						else
						{
							self.dispatchEvent(new SmitearWebGL.Events.IOErrorEvent( SmitearWebGL.Events.IOErrorEvent.IO_ERROR, this.status ) );
							$WARNING( "Error loading Document: " + url + " status " + this.status );
						}
					}
				}
				;
				req.open( "GET", docurl, false );
				req.send( "" );
			}
		}
	}
	,
	_loadDocument : function( node )
	{
		this._ignoreWhiteSpace( node );
		
		this.document = new SmitearWebGL.Objects.Parser.Collada.DaeDocument( node );
		this.document.baseUrl = this.baseUrl;

		SmitearWebGL.Objects.Parser.Collada.ColladaDocuments[this.url] = this.document;

		this._sourceTimer.start();

		return this.document;
	}
	,
	_ignoreWhiteSpace : function( node )
	{
		var child=node.firstChild;
		var nextSibling;
		
		if ( !child ) return;
		
		do
		{
			nextSibling = child.nextSibling;
			
			if( child.nodeName == "#text" && ! /\S/.test( child.nodeValue ) )
			{
				node.removeChild( child );
			}
			else
			{
				this._ignoreWhiteSpace( child );
			}
		}
		while( child = nextSibling );
	}
	,
	loadNextSource : function( event )
	{
		var num = this.document.getWaitingSources() ? this.document.getWaitingSources().length : 0;

		if( this.document.readNextSource() )
		{
			this.dispatchEvent( new SmitearWebGL.Events.ProgressEvent( SmitearWebGL.Events.ProgressEvent.PROGRESS, this.document.numSources-num, this.document.numSources ) );
		}
		else
		{
			this._sourceTimer.stop();
			if( this._sourceTimer.hasEventListener( SmitearWebGL.Events.TimerEvent.TIMER ) )
			{
				this._sourceTimer.removeEventListener( SmitearWebGL.Events.TimerEvent.TIMER, this.loadNextSource );
			}

			this.document.addEventListener( SmitearWebGL.Events.Event.COMPLETE, this, this._onImagesComplete );

			this.parseMessage = "reading images";
			this.document.readNextImage();
		}
	}
	,
	_onImagesComplete : function( event )
	{
		this.document.readAfterSources();

		if( this.document.hasEventListener( SmitearWebGL.Events.Event.COMPLETE ) )
		{
			this.document.removeEventListener( SmitearWebGL.Events.Event.COMPLETE, this._onImagesComplete );
		}

		this.dispatchEvent( new SmitearWebGL.Events.Event( SmitearWebGL.Events.Event.COMPLETE ) );
	}
}
);

SmitearWebGL.Objects.Parser.Collada.ColladaDocuments = {};